package service

import (
	"IMChat/models"
	"IMChat/utils"
	"github.com/gin-gonic/gin"
	"strconv"
)

func ChatList(c *gin.Context) {
	roomIdentity := c.Query("room_identity")
	if roomIdentity == "" {
		utils.FailWithMsg(c, -1, "room identity cannot be null")
		return
	}
	uc := c.MustGet("user_claims").(*utils.UserClaims)
	_, err := models.GetUserRoomByUserIdentityRoomIdentity(uc.Identity, roomIdentity)
	if err != nil {
		utils.FailWithMsg(c, -1, "Illegal Visit")
		return
	}
	pageIndex, _ := strconv.ParseInt(c.Query("page_index"), 10, 32)
	pageSize, _ := strconv.ParseInt(c.Query("page_size"), 10, 32)
	skip := (pageIndex - 1) * pageSize
	data, err := models.GetMessageListByRoomIdentity(roomIdentity, &pageSize, &skip)
	if err != nil {
		utils.FailWithMsg(c, -1, "[System Error]:"+err.Error())
		return
	}
	utils.OkWithData(c, data)
	return
}
